package com.example.exam.MeunFrgment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.exam.R;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;

/**
 * A simple {@link Fragment} subclass.
 */
public class RetrofitFragment extends Fragment {


    @BindView(R.id.retrofit_text)
    TextView retrofitText;
    private View view;

    public RetrofitFragment() {
        // Required empty public constructor
    }

    private interface BlogSever{
        @GET("carlist")
        Call<ResponseBody> getBlog();
        @GET("getTrafficLightRGY")
        Call<ResponseBody> getTrafficLightRGY();
        @POST("pubcar")
        Call<ResponseBody> getPubCar(@Body RequestBody str);

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_retrofit, container, false);
        ButterKnife.bind(this,view);
        initData();
        return view;

    }

    private void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        BlogSever blogSever = retrofit.create(BlogSever.class);
        Map<String,Object> map = new HashMap<>();
        map.put("CarId",1);
        String s = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"),s);
        Call<ResponseBody> pubCar = blogSever.getPubCar(requestBody);
        pubCar.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String string = response.body().string();
                    retrofitText.append("\n***********\n"+string);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
        Call<ResponseBody> trafficLightRGY = blogSever.getTrafficLightRGY();
        trafficLightRGY.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String string = response.body().string();
                    retrofitText.append("\n***********\n"+string);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
        Call<ResponseBody> blog = blogSever.getBlog();
        blog.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String string = response.body().string();
                    retrofitText.append("\n***********\n"+string);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

}
